const API = require('../../utils/api')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    console.log('用户资料',app.globalData.userInfo)
    this.setData({
      userInfo:app.globalData.userInfo,
      hasUserInfo:app.globalData.hasUserInfo
    })
  },

  /**
   * 登陆
   */
  login:function(){
    let that = this
    app.getUserInfo(getUserInfo, this.data.phoneNumber)
    function getUserInfo(data){
      that.setData({
        userInfo:data,
        hasUserInfo:true
      })
    }
  },

  /**
   * 获取手机号
   */
  getPhoneNumber:function(e){
    let that = this
    wx.login({
      success (res) {
        if (res.code) {
          API.get_mobile({
            appid:app.globalData.appid,
            encryptedData:e.detail.encryptedData,
            iv:e.detail.iv,
            code:res.code
          },app.globalData.appid).then( res => {
            if( res.code == 200 ){
              that.setData({
                phoneNumber: res.data.phoneNumber
              })
            }else{
              wx.showToast({
                title: '授权登陆失败，请稍后重试',
                icon:'none'
              })
            }
          })
        }
      }
    })
  },

  /**
   * 路由 - 我的资料
   */
  routeMyInfo:function(){
    wx.navigateTo({
      url: '/pages/mine/info/info',
    })
  },

  /**
   * 统一菜单路由
   */
  route:function(e){
    let type = e.currentTarget.id
    if( !this.data.hasUserInfo ){
      wx.showToast({
        title: '您还没有登陆哦',
        icon: 'none'
      })
      return
    }
    console.log( type )
    if( type == 1 ){ //邀请团员
      wx.navigateTo({
        url: '/pages/home/marketing/marketing',
      })
    }
    else if( type == 2 ){ //我的推广
      wx.navigateTo({
        url: '/pages/mine/share/share',
      })
    }
    else if( type == 3 ){ //城市合伙人
      wx.navigateTo({
        url: '/pages/mine/agent/agent',
      })
    }
    else if( type == 4 ){ //消息中心
      wx.navigateTo({
        url: '/pages/mine/message/message',
      })
    }
    else if( type == 5 ){ //投诉建议
      wx.navigateTo({
        url: '/pages/mine/proposer/proposer',
      })
    }
  }
})